package mytest.graph;

import java.util.Stack;

public class Graph{
	private final int Max_Size = 20;
	private Vertex vertexList[] ;
	private int adjMat[][];
	private Stack theStack;
	private int nVerts ;
	public Graph(){
		vertexList = new Vertex[Max_Size];
		adjMat = new int[Max_Size][Max_Size];
		nVerts = 0;
		for(int i = 0;i<Max_Size;i++){
			for(int k = 0;k<Max_Size;k++){
				adjMat[i][k] = 0;
			}
		}
		theStack = new Stack();
	}
	
	public void addVertex(String label){
		vertexList[nVerts++] = new Vertex(label); 
	}
	
	public void addEdge(int start,int end){
		adjMat[start][end] = 1;
		adjMat[end][start] = 1;
	}
	
	public void displayVertex(int v){
		System.out.println(vertexList[v].label);
	}
	
	
	public int getAdjUnvisitedVertex(int v){
		
		for(int j=0;j<nVerts;j++){
			if(adjMat[v][j] == 1 && vertexList[j].wasVisited == false )
				return j;
		}
		return -1;
	}
	
	                                                                                                                                                                                                                                                                                            
	public void dsf(){
		vertexList[0].wasVisited = true;
		this.displayVertex(0);
		theStack.push(0);
		
		while(!theStack.empty()){
			int peek = (Integer)theStack.peek();
			int v = this.getAdjUnvisitedVertex(peek);
			if(v == -1){
				theStack.pop();
			}else{
				vertexList[v].wasVisited = true;
				this.displayVertex(v);
				theStack.push(v);
			}
			
		}
		//
		for(int j = 0;j<nVerts;j++)
			vertexList[j].wasVisited = false;
		
	}
	
	
	
	
	
	
}